package com.baikeyang.service.activity.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baikeyang.common.model.ActivityUserCare;
import com.baikeyang.mapper.ActivityUserCareMapper;
import com.baikeyang.service.activity.IActivityUserCareService;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户关注 服务实现类
 * </p>
 *
 * @author BaiKeyang
 * @since 2020-05-24
 */
@Service
public class IActivityUserCareServiceImpl extends SuperServiceImpl<ActivityUserCareMapper, ActivityUserCare> implements IActivityUserCareService {

    @Autowired
    private ActivityUserCareMapper activityUserCareMapper;

    @Override
    public int saveActivityUserCare(ActivityUserCare userCare) {
        userCare.setCreateTime(new Date());
        userCare.setCreateUser(0L);
        userCare.setUpdateTime(new Date());
        userCare.setCreateUser(0L);
        return activityUserCareMapper.insert(userCare);
    }

    @Override
    public int getActivityUserCareByUserIdAndCareUserId(Long userId, Long careUserId) {
        QueryWrapper<ActivityUserCare> queryWrapper = Wrappers.query();
        queryWrapper.eq("USER_ID", userId);
        queryWrapper.eq("CARE_USER_ID", careUserId);
        List<ActivityUserCare> activityUserCares = activityUserCareMapper.selectList(queryWrapper);
        return activityUserCares.size();
    }

    @Override
    public int delActivityUserCareByUserIdAndCareUserId(String userId, Long careUserId) {
        QueryWrapper<ActivityUserCare> queryWrapper = Wrappers.query();
        queryWrapper.eq("USER_ID", userId);
        queryWrapper.eq("CARE_USER_ID", careUserId);
        List<ActivityUserCare> activityUserCares = activityUserCareMapper.selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(activityUserCares)){
            activityUserCareMapper.deleteById( activityUserCares.get(0).getId() );
        }
        return 0;
    }
}
